package com.lightboat.common.api;

import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.lightboat.common.core.domain.bo.Credential;
import com.lightboat.common.utils.appSigner.BaiduAppSigner;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.io.Serializable;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
public class BaiDuJianKangApi {

    public static final String BAIDU_JIAN_KANG_URL = "https://jiankang.baidu.com/seller-open/";


    /**
     * 订单详情
     */
    public static final String ORDER_DETAIL = BAIDU_JIAN_KANG_URL + "order/detail";

    /**
     * 订单列表
     */
    public static final String ORDER_LIST = BAIDU_JIAN_KANG_URL + "order/list";

    /**
     * 订单物流修改
     */
    public static final String ORDER_DELIVER_UPDATE = BAIDU_JIAN_KANG_URL + "order/updateDeliverInfo";

    /**
     * 商品列表
     */
    public static final String GOODS_LIST = BAIDU_JIAN_KANG_URL + "goods/list";

    /**
     * B2C订单发货(批量)
     */
    public static final String ADD_DELIVER_LIST = BAIDU_JIAN_KANG_URL + "order/addDeliverList";
    /**
     * 售后详情
     */
    public static final String AFTERSALE_DETAIL = BAIDU_JIAN_KANG_URL + "afterSale/detail";


    /**
     * 售后列表
     */
    public static final String AFTERSALE_LIST = BAIDU_JIAN_KANG_URL + "afterSale/list";


    public static JSONObject send(Credential credential, Map<String, String> headers, String url, String data) {
        Map<String, String> signHeaders = BaiduAppSigner.getSign(credential, url, true);
        if (headers != null) {
            signHeaders.putAll(headers);
        }
        log.debug("signHeaders: {}", JSON.toJSONString(signHeaders));
        try (HttpResponse execute = HttpUtil.createPost(url).headerMap(signHeaders, true).body(data).timeout(3000).execute()) {
            return JSON.parseObject(execute.body());
        } catch (Exception e) {
            log.error("发送请求异常:{},当前请求数据：{}", url, data, e);
            return null;
        }
    }


    /**
     * orderId	订单No	body	true	string	1011663054639320322
     * storeId	门店ID	body	true	long	160612698269212343
     */
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class OrderDetailRequest {
        // 商户ID
        private String storeId;
        // 订单ID
        private String orderId;
    }

    /**
     * 订单列表请求
     */
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class OrderListRequest {
        /**
         * 下单时间开始(ms级时间戳)
         */
        private Long orderTimeFrom;
        /**
         * 下单时间结束(ms级时间戳)
         */
        private Long orderTimeTo;
        /**
         * 修改时间开始(ms级时间戳)
         */
        private Long updateTimeFrom;
        /**
         * 修改时间结束(ms级时间戳)
         */
        private Long updateTimeTo;
        /**
         * 支付时间开始(ms级时间戳)
         */
        private Long payTimeStart;
        /**
         * 支付时间结束(ms级时间戳)
         */
        private Long payTimeEnd;
        /**
         * 订单状态
         * 130	交易关闭
         * 140	购药订单：用户已支付，待商家发货；
         * 143	商家已接单
         * 146	商家拒绝接单
         * 150	购药订单商家已发货
         * 160	用户申请退款，待商家审核
         * 190	B2C订单为已签收 （物流轨迹返回签收状态）; O2O订单为商家确认送达
         * 200	购药订单：用户确认收货
         * 999	其他状态
         */
        private Integer status;
        /**
         * 店铺ID
         */
        private Long shopId;
        /**
         * 门店ID
         */
        private Long storeId;
        /**
         * 订单类型 1: 购药 2:疫苗 3:检测
         */
        private Integer type;
        /**
         * 是否是处方订单,1:是 0:否
         */
        private Integer chufangFlag;
        /**
         * 分页参数
         */
        private PageRequest page;
    }

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class DataRequest<T> {
        private T data;
    }

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class PageRequest {
        private Boolean desc;
        private Integer page;
        private Integer size;
        // 排序字段,可用值: 1, 下单时间
        private Integer orderBy;
    }


    @Data
    public static class PageResponse {
        private Long total;
        private Integer page;
        private Integer size;
        private List<String> list;
    }

    @Data
    public static class OrderDetailResponse implements Serializable {
        private String orderId;

        private Integer discount;

        private List<GoodsResponse> goods;

        private JSONArray deliverInfos;

        private String channelType;

        private Integer insuranceType;

        private Integer type;

        private String packageType;

        private Integer payType;

        private String addressDetail;

        private Integer preferentialPriceFen;

        private String province;

        private Integer processAmount;

        private String receMobile;

        private String shopId;

        private String clearanceIdCard;

        private Long payTimeOut;
        private Long payTime;
        /**
         * 配送完成时间
         */
        private Long deliverReceTime;
        /**
         * 配送开始时间
         */
        private Long deliverSendTime;

        private String longitude;

        private String townName;

        private Long userPayment;

        private Integer dosageForm;

        private SellerInfo sellerInfo;

        private String receMessage;

        private Integer companyId;

        private String receAddressDetail;

        private Integer paidAmountFen;

        private String district;

        private String deliverName;

        private Integer status;

        private String receName;

        private String personalPhone;

        private String city;

        private String latitude;

        private String deliverNo;

        private String remark;

        private Integer vaccWhite;

        private String clearanceName;

        private Integer shouldPayAmount;

        private String tpStoreCode;

        private Integer bdDiscount;

        private String storeName;

        private Integer processType;

        private Integer originalPriceFen;

        private String payName;

        private String channelCode;

        private String storeId;

        private Integer chufangFlag;

        private Long createTime;

        private Integer goodsPriceFen;

        private String personalName;

        /**
         * 用户key
         */
        private String userKey;

        private Integer tpPaymentFen;

        private Long nmId;

        private String tpNmId;

        private String tpOrderId;

        private String message;

        private Integer deliveryPriceFen;

        WarehouseInfo warehouseInfo;

        private String name;

        private Integer sendType;


        @Data
        public static class SellerInfo implements Serializable {
            private Integer companyNature;

            private Integer businessType;

            private Integer sellerType;
        }

        @Data
        public static class WarehouseInfo implements Serializable {
            private Integer deliverMode;

            private String warehouseName;

            private String warehouseNO;
        }
    }


    @Data
    public static class GoodsResponse implements Serializable {
        private Long goodsId;

        private String companyName;

        private Integer tpDiscount;

        private String businessName;

        private String tpSkuId;

        private Integer discount;

        private String spec;

        private String oldGoodsId;

        private Integer bdDiscount;

        private String storeName;

        private String tpSkuCode;

        private String licenseId;

        private String skuId;

        private Long priceFen;

        private String categoryLevel1;

        /**
         * 数量
         */
        private Integer amount;

        private String categoryLevel3;

        private String categoryLevel2;

        private Long actualPaymentFen;

        private Integer combineDiscount;

        private CombineGoodsInfo combineGoodsInfo;

        private Integer goodsType;

        private String titleName;

        private String headImgUrl;

        private Long userPayment;

        private List<SellerDiscount> sellerDiscount;

        /**
         * 合并商品信息
         */
        @Data
        @AllArgsConstructor
        @NoArgsConstructor
        public static class CombineGoodsInfo implements Serializable {
            private Integer amount;

            private String title;

            private Long skuId;
        }

        /**
         * 卖家折扣
         */
        @Data
        @AllArgsConstructor
        @NoArgsConstructor
        public static class SellerDiscount implements Serializable {
            private String id;

            private Integer amountFen;

            /**
             * 折扣类型 0平台卷 1商家卷 2组合优惠 3催付优惠
             */
            private Integer type;

            private String title;
        }
    }


    @Data
    public static class GoodsListRequest extends PageRequest implements Serializable {

        /**
         * 审核状态 04审核拒绝 2审核通过 5审核中
         */
        private Integer auditStatus;

        /**
         * 商品id
         */
        private List<String> goodsIds = new ArrayList<>();

        /**
         * 显示状态  1显示中 2 屏蔽中
         */
        private Integer showStatus;

        private String tpSkuCode;

        private Integer status;

        private Long stock;

        private Long storeId;

        private Long barcode;

    }


    @Data
    public static class GoodsDetailResponse implements Serializable {
        private String commonName;

        private String brandName;

        private Long goodsId;

        private String title;

        /**
         * 商品类型（1中西药品，2医疗器械,3保健品,4成人用品,5中药养生,6美妆个护,7母婴系列,8食品百货,9疫苗信息）
         */
        private Integer type;

        private String spec;

        private List<String> pictures;

        private List<String> articlePictures;

        private JSONObject attributeInfo;

        private Integer goodStatus;
        /**
         * 商品类型（0普通商品，1组合商品）
         */
        private Integer goodsType;

        private List<String> headerImages;

        private SkuInfo skuInfo;

        private String standardInfoId;

        @Data
        public static class SkuInfo implements Serializable {
            private Long stockSize;

            private Long marketPriceFen;

            private Long storeId;

            private String tpSkuCode;
            private String name;

            private Long skuId;

            /**
             * 价格
             */
            private Long priceFen;
            /**
             * 销量
             */
            private Long salesVolume;

            private String spec;

            /**
             * 标题图片
             */
            private List<String> headerImages;
            /**
             * 图片
             */
            private List<String> pictures;
            /**
             * 文章图片
             */
            private List<String> articlePictures;
            private List<String> images;
            private List<String> videos;
        }

    }

    /**
     * 医疗设备属性信息
     */
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class MedicalDevicesAttributeInfo implements Serializable {
        /**
         * 国产/进口标识 1: 国产 2:进口
         */
        private Integer abroadHome;

        /**
         * 品牌/企业简称
         */
        private String brandName;

        /**
         * 生产厂家地址
         */
        private String businessAddress;

        /**
         * 生产厂家名称
         */
        private String businessName;

        /**
         * 产品名称
         */
        private String commonName;

        /**
         * 商品产地
         */
        private String goodsOrigin;

        /**
         * 注册证号/备案编号
         */
        private String licenseId;

        /**
         * 注意事项
         */
        private String notice;

        /**
         * 产品型号
         */
        private String productModel;

        /**
         * 注册证人名称
         */
        private String registCertificatePersonName;

        /**
         * 产品注册证图片/产品备案图片
         */
        private List<String> registCertificatePicture;

        /**
         * 注册证有效期(结束)
         */
        private LocalDate registCertificateValidityEnd;

        /**
         * 注册证有效期(开始)
         */
        private LocalDate registCertificateValidityStart;

        /**
         * 器械结构及组成
         */
        private String structuralComposition;

        /**
         * 适用人群
         */
        private String suitablePeople;

        /**
         * 使用方法
         */
        private String usageMethod;

        /**
         * 使用场景
         */
        private String usageScenarios;

        /**
         * 产品类别：1 一级；2 二级；3 三级
         */
        private Integer registType;
    }


    /**
     * 中西药品属性信息
     */
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class ChineseWesternMedicinesAttributeInfo implements Serializable {
        /**
         * 1: 国产/2:进口
         */
        private Integer abroadHome;

        /**
         * 禁忌
         */
        private String abstainFrom;

        /**
         * 不良反应
         */
        private String adverseReactions;

        /**
         * 品牌/企业简称
         */
        private String brandName;

        /**
         * 性状
         */
        private String characters;

        /**
         * 化学名
         */
        private String chemName;

        /**
         * 处方类型, 1:处方药, 0:非处方药
         */
        private Integer chufangFlag;

        /**
         * 包装方法
         */
        private String cover;

        /**
         * 最大可购买盒数（处方药特有）
         */
        private Integer dayLimitNum;

        /**
         * 主治疾病标签（处方药特有
         */
        private List<Disease> diseases;

        /**
         * 药物用量
         */
        private String drugDosage;

        /**
         * 相互作用
         */
        private String drugInteract;

        /**
         * 执行标准
         */
        private String drugIso;

        /**
         * 药物药理
         */
        private String drugPharTox;

        /**
         * 儿童用药
         */
        private String forChildUsed;

        /**
         * 老年用药
         */
        private String forOldUsed;

        /**
         * 孕期和孕期用药方法
         */
        private String forPregnantUsed;

        /**
         * 适应病症
         */
        private String indication;

        /**
         * 成分
         */
        private String material;

        /**
         * 注意事项
         */
        private String notice;

        /**
         * 药物过量
         */
        private String overUse;

        /**
         * 拼音学名
         */
        private String pyName;

        /**
         *
         */
        private Long spuId;

        /**
         * 贮藏方法
         */
        private String storeWay;

        /**
         * 适用人群
         */
        private String suitablePeople;

        /**
         * 药物毒理
         */
        private String toxicity;

        /**
         * 不宜人群
         */
        private String unfitPeople;

        /**
         * 用法用量
         */
        private String usage;

        /**
         * 有效期
         */
        private String validityDate;
    }

    /**
     * 保健品属性信息
     */
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class SupplementsAttributeInfo implements Serializable {
        /**
         * 1: 国产/2:进口
         */
        private Integer abroadHome;

        /**
         * 品牌/企业简称
         */
        private String brandName;

        /**
         * 生产厂家地址
         */
        private String businessAddress;

        /**
         * 生产厂家名称
         */
        private String businessName;

        /**
         * 产品名称
         */
        private String commonName;

        /**
         * 剂型
         */
        private String dosageForm;

        /**
         * 食品生产许可证编号
         */
        private String foodLicenseCode;

        /**
         * 产品类别
         */
        private String goodsType;

        /**
         * 保质期
         */
        private String guaranteePeriod;

        /**
         * 功效与作用
         */
        private String indication;

        /**
         * 批准文号
         */
        private String licenseId;

        /**
         * 成分
         */
        private String material;

        /**
         * 净含量
         */
        private String netContent;

        /**
         * 适用人群
         */
        private String suitablePeople;

        /**
         * 用法用量
         */
        private String usage;

        /**
         * 有效期
         */
        private String validityDate;
    }

    /**
     * 中药养生属性信息
     */
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class HealthAttributeInfo implements Serializable {
        /**
         * 品牌/企业简称
         */
        private String brandName;

        /**
         * 产品名称
         */
        private String commonName;

        /**
         * 商品产地
         */
        private String goodsOrigin;

        /**
         * 保质期
         */
        private String guaranteePeriod;

        /**
         * 功效与作用
         */
        private String indication;

        /**
         * 成分
         */
        private String material;

        /**
         * 净含量
         */
        private String netContent;

        /**
         * 贮藏
         */
        private String storeWay;

        /**
         * 适用人群
         */
        private String suitablePeople;

        /**
         * 用法用量
         */
        private String usage;
    }

    /**
     * 主治疾病标签（处方药特有）
     */
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class Disease implements Serializable {
        /**
         * 主治疾病标签（处方药特有）
         */
        private String disease;

        /**
         * 权重
         */
        private Integer weight;
    }


    /**
     * 订单发货(批量) 请求
     */
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class AddDeliverListRequest implements Serializable {
        private List<DeliverBatch> deliverBatch;

        /**
         * 订单编号
         */
        private String orderId;

        /**
         * 物流ID(快递单号)
         */
        private String deliverNo;

        /**
         * 门店Id
         */
        private String storeId;

        /**
         * 物流商编码
         */
        private String deliverName;

        /**
         * 店铺Id
         */
        private String shopId;

        @Data
        @AllArgsConstructor
        @NoArgsConstructor
        public static class DeliverBatch implements Serializable {
            private List<BatchInfo> batchInfos;

            /**
             * 商品Id,必传
             */
            private Long skuId;


        }

        @Data
        @AllArgsConstructor
        @NoArgsConstructor
        public static class BatchInfo implements Serializable {
            private String expiryDate;

            private String batchNo;
        }
    }


    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class DeliverResponse implements Serializable {

        /**
         * 失败原因
         */
        private String reason;

        /**
         * 公司 ID
         */
        private Integer companyId;

        /**
         * 订单编号
         */
        private String orderId;

        /**
         * 物流ID(快递单号)
         */
        private String deliverNo;

        /**
         * 门店Id
         */
        private String storeId;

        /**
         * 物流商编码
         */
        private String deliverName;

        /**
         * 是否发货成功，1成功，2失败
         */
        private Integer resultType;
    }


    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class CommentRequest implements Serializable {
        private String sellerId;

        private Integer size;

        private Integer defPraise;

        private String startTime;

        private String shopId;

        private Integer page;

        private String endTime;
    }


    /**
     * 售后详情请求
     */
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class AfterSaleDetailRequest implements Serializable {
        /**
         * 售后单号
         */
        private String afterSaleId;

        /**
         * 门店Id
         */
        private String storeId;
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class AfterSaleDetailResponse implements Serializable {
        private String returnDeliverName;

        private String reason;

        private String orderId;

        private Long payTime;

        private String description;

        private Integer orderStatus;

        private String afterSaleId;

        private Long createOrderTime;

        private Long storeId;

        /**
         * 售后类型 1：仅客诉 2：仅退款 3：退货退款
         */
        private Integer type;

        private String returnPhone;

        private Long returnTime;

        /**
         * 	退单类型 1：子单 2：整单
         */
        private Integer refundType;

        private Long expireTime;

        private List<AfterSaleDeliverInfo> afterSaleDeliverInfo;

        private String returnDeliverNo;

        /**
         * 	订单发货状态 1：未发货 2：已发货
         */
        private Integer sendStatus;

        private Long shopId;

        private Long applyTime;

        private List<GoodsInfo> goods;

        /**
         * 退款金额
         */
        private Long refundPriceFen;
        /**
         * 	运费
         */
        private Long deliveryPriceFen;

        private List<String> imageList;

        private Integer status;

        @Data
        public static class AfterSaleDeliverInfo implements Serializable {
            private String description;

            private Long time;
        }

        @Data
        public static class GoodsInfo implements Serializable {
            private Integer amount;

            private String titleName;

            private Long refundPaymentFen;

            private Integer userPayment;

            private String headImgUrl;

            private Long tpDiscount;

            private Long bdDiscount;

            private Long discount;

            private Long totalPaymentFen;

            private String tpSkuId;
            /**
             * 主品id（组合商品、疗程商品的主品skuId）用于匹配sku绑定的售后地址id
             */
            private String groupSkuId;

            private String skuId;
        }
    }

    /**
     * 售后单推送消息数据传输对象
     */
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class AfterSalePushMessageDTO implements Serializable{

        /**
         * 售后单ID
         */
        private Long id;

        /**
         * 售后单状态
         */
        private Integer status;

        /**
         * 门店ID
         */
        private Long storeId;

        /**
         * 店铺ID
         */
        private Long shopId;

        /**
         * 商家门店ID
         */
        private String tpStoreCode;

        /**
         * 售后单逾期时间（可能为空）
         */
        private Long expireTime;

        /**
         * 售后单申请时间
         */
        private Long applyTime;

        /**
         * 售后单类型
         * status	类型	含义
         * 1000	申请售后单（待审核）	申请售后待商家审核
         * 1160	用户寄回（待审核）	用户寄回待商家审核
         * 1300	退款中	售后单退款中
         * 1400	退款成功	售后单退款成功
         * 1310	退款失败	售后单退款失败
         * 1100	用户取消售后	用户取消售后
         * 1200	售后关闭	售后关闭
         */
        private Integer afterSaleType;

        /**
         * 订单ID
         */
        private String orderId;
    }

}
